﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace Brianhdk.Utils.VisualStudio.AssemblyInfoDialog
{
    internal static class AssemblyInformationCollector
    {
        public static IEnumerable<AssemblyInformation> Collect(AssemblyName assembly)
        {
            yield return new AssemblyInformation("Name", assembly.Name);
            yield return new AssemblyInformation("Full Name", assembly.FullName);
            yield return new AssemblyInformation("Version", assembly.Version.ToString());

            yield return new AssemblyInformation("Public Key Token", FormatBytes(assembly.GetPublicKeyToken()));
            yield return new AssemblyInformation("Public Key", FormatBytes(assembly.GetPublicKey()));

            var fileInfo = GetFileInfo(assembly);
            yield return new AssemblyInformation("File Path", fileInfo.FullName);
            yield return new AssemblyInformation("Directory", fileInfo.DirectoryName);
        }

        private static FileInfo GetFileInfo(AssemblyName assembly)
        {
            var uri = new Uri(assembly.CodeBase);

            return new FileInfo(uri.LocalPath);
        }

        private static string FormatBytes(byte[] bytes)
        {
            if (bytes == null || bytes.Length == 0)
                return "<n/a>";

            return
                String.Join(
                    String.Empty,
                    bytes.Select(x => x.ToString("x2")));
        }
    }
}